const { resolveApp } = require('./resolve');
const package = require(`${resolveApp('')}/package.json`);
const fs = require('fs');
const globalPkgPath = `${resolveApp('')}/node_modules/@seeyon/global/package.json`;

let globalVersion = '1.0.6';

if (fs.existsSync(globalPkgPath)) {
  globalVersion = require(globalPkgPath).version;
}

function createLibraryInfo() {
  const isProduction = process.env.NODE_ENV === 'production';
  const isMobile = package.name.includes('mobile');
  const { commonDependencies } = package;

  let reactPath = '/static/vendor.dev.js';
  let uiPath = isMobile ? '/static/seeyonmui.dev.js' : '/static/seeyonui.dev.js';
  const globalPath = isMobile
    ? `/static/mobile.global.${globalVersion}.js`
    : `/static/desktop.global.${globalVersion}.js`;
  // const external = isMobile ? { '@seeyon/mui': 'SeeyonMUI' } : { '@seeyon/ui': 'SeeyonUI' };
  const external = isMobile
    ? {}
    : {
        '@seeyon/ui-business-global-frame-layout': 'SeeyonUiBusinessGlobalFrameLayout',
        '@seeyon/ui-business-org-selector': 'SeeyonUiBusinessOrgSelector',
        '@seeyon/ui-business-print': 'SeeyonUiBusinessPrint',
        '@seeyon/ui-business-search': 'SeeyonUiBusinessSearch',
      };

  if (isProduction) {
    reactPath = `/static/vendor.${commonDependencies['vendor']}.js`;
    uiPath = isMobile
      ? `/static/seeyonmui.${commonDependencies['@seeyon/mui']}.js`
      : `/static/seeyonui.${commonDependencies['@seeyon/ui']}.js`;
  }

  return {
    libraryPaths: [
      reactPath,
      `/static/seeyonui.${commonDependencies['@seeyon/ui']}.js`,
      globalPath,
    ],
    external: {
      '@seeyon/ui': 'SeeyonUI',
      '@seeyon/global': 'SeeyonGlobal',
      '@seeyon/tools': 'SeeyonTools',
      ...external,
    },
  };
}

module.exports = createLibraryInfo;
